package com.demo.business.cdkey;

import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 
 * @create: 2022-02-25 23:30:03
 **/
@Service("cdkey")
@Slf4j
public class CdkeyService{

    @Resource
    CdkeyDao cdkeyDao;

    /**
     * 添加,获取lId数据
     *
     * @param cdoRequest - lId:id
     *                   - sCDKey:CDKey码
     *                   - sCDKName:CDK名称
     *                   - sGetWay:获得方式
     *                   - lUserId:用户id
     *                   - lActiveUserId:激活用户id
     *                   - nAttribute:属性：1/2/3:一日/三日/七日VIP卡,99用户分享的CDK类型，-99使用别人的分享CDK码类型
     *                   - nType:类型：0绑定，1 不记名，99、分享的CDK码，-99使用别人分享的CDK码
     *                   - nStatus:状态：0未激活，1 已激活，99 失效
     *                   - dValidTime:有效时间
     *                   - dInvalidTime:失效时间
     *                   - dActiveTime:激活时间
     *                   - dCreateTime:创建时间
     *                   - sShareShortUrl:
     *                   - sShareQRcodeUrl:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    @OpenApi
    public Return addCdkey(CDO cdoRequest) {
        cdkeyDao.addCdkey(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("lId")) {
            cdoResult.set("lId", cdoRequest.get("lId"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lId，更新数据
     *
     * @param cdoRequest - lId:id
     *                   - sCDKey:CDKey码
     *                   - sCDKName:CDK名称
     *                   - sGetWay:获得方式
     *                   - lUserId:用户id
     *                   - lActiveUserId:激活用户id
     *                   - nAttribute:属性：1/2/3:一日/三日/七日VIP卡,99用户分享的CDK类型，-99使用别人的分享CDK码类型
     *                   - nType:类型：0绑定，1 不记名，99、分享的CDK码，-99使用别人分享的CDK码
     *                   - nStatus:状态：0未激活，1 已激活，99 失效
     *                   - dValidTime:有效时间
     *                   - dInvalidTime:失效时间
     *                   - dActiveTime:激活时间
     *                   - dCreateTime:创建时间
     *                   - sShareShortUrl:
     *                   - sShareQRcodeUrl:
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    @OpenApi
    public Return updateCdkeyById(CDO cdoRequest) {
        int affectedRows = cdkeyDao.updateCdkeyById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据lId，删除数据
     *
     * @param cdoRequest - lId:id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    //@OpenApi
    public Return deleteCdkeyById(CDO cdoRequest){
        int affectedRows = cdkeyDao.deleteCdkeyById(cdoRequest.getLong("lId"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除数据
     *
     * @param cdoRequest - sCDKey:CDKey码
     *                   - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    //@OpenApi
    public Return deleteCdkey(CDO cdoRequest) {
        int affectedRows = cdkeyDao.deleteCdkey(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取数据
     *
     * @param cdoRequest - lId:id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    @OpenApi
    public Return getCdkeyById(CDO cdoRequest) {
        CDO cdoMapperResult = cdkeyDao.getCdkeyById(cdoRequest.getLong("lId"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取数据
     *
     * @param cdoRequest - lId:id
     *                   - sCDKey:CDKey码
     *                   - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    @OpenApi
    public Return getCdkey(CDO cdoRequest) {
        CDO cdoMapperResult = cdkeyDao.getCdkey(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取数据列表
     *
     * @param cdoRequest - sCDKey:CDKey码
     *                   - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    @OpenApi
    public Return getCdkeyList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = cdkeyDao.getCdkeyList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = cdkeyDao.getCdkeyListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取数据列表
     *
     * @param cdoRequest - lId:id
     *                   - sCDKey:CDKey码
     *                   - lUserId:用户id
     * @return
     * @author 
     * @create: 2022-02-25 23:30:03
     */
    @OpenApi
    public Return getCdkeyListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = cdkeyDao.getCdkeyListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = cdkeyDao.getCdkeyListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
